package com.qf.front.controller;

import com.qf.front.biz.GoodsBiz;
import com.qf.front.biz.OrderService;
import com.qf.front.task.OrderTimer;
import com.qf.ym.anno.AuthCheckAnno;
import com.qf.ym.entity.*;
import com.qf.ym.finalstr.FruitsFinal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
public class OrderController {
    @Autowired
    OrderService orderService;

    @Autowired
    GoodsBiz goodsBiz;



    @RequestMapping("jieSuan")
    @AuthCheckAnno
    public String jieSuan(int[] goodscheck, String type, HttpServletRequest request){
        float s=0;
        if("now".equals(type)){//立即结算所有的订单
            //获取购物车
            Map<Integer, GoodsEntity> shopCar = (Map<Integer, GoodsEntity>)request.getSession().getAttribute("shopCar");
            //购物车所有商品id组成的字符数组
            Set<Integer> integers = shopCar.keySet();

            for(Integer gid:integers ){
                GoodsEntity goodsEntity = goodsBiz.queryGoodsDetail(gid);
                float v = goodsEntity.getGoodsNumber() * goodsEntity.getGoodsPrice();
                s+=v;
            }


            request.getSession().setAttribute("s",s);
            return "redirect:showAddress";

        }
        return null;
    }


    //添加地址信息
    @RequestMapping("addAddress")
    @ResponseBody
    public ResponseResult addAddress(AddressEntity ad, HttpServletRequest request){
        ResponseResult rr = new ResponseResult();
        UserEntity user = (UserEntity)request.getSession().getAttribute("user");
        int aid=user.getAid();
        ad.setAid(aid);
        boolean b = orderService.addAddress(ad);
        if(b){
            rr.setResult(FruitsFinal.SUCCESS_RESULT);

        }else {
            rr.setResultMsg("保存失败");
        }
        return rr;
    }

    //展示存在的地址信息
    @RequestMapping("showAddress")
    public String showAddress(HttpServletRequest request){
        UserEntity user = (UserEntity)request.getSession().getAttribute("user");
        int aid=user.getAid();
        List<AddressEntity> list=orderService.showAddress(aid);
        if(list==null){
            return "accounts";
        }
        request.setAttribute("list",list);

        return "accounts";
    }

    //支付,生成订单
    @RequestMapping("createOrder")
    public  String createOrder(HttpServletRequest request,Integer adr_radio){
        float s = (float)request.getSession().getAttribute("s");
        Timer timer = new Timer();


        UserEntity user = (UserEntity)request.getSession().getAttribute("user");
        int aid=user.getAid();

        if(adr_radio==null){
            request.setAttribute("msg","未选择收货地址");
            return "redirect:showAddress";
        }

        OrderEntity oe = new OrderEntity();
        oe.setOtotalPrice(s);
        oe.setAddressId(adr_radio);
        oe.setAid(aid);
        //获取购物车
       Map<Integer, GoodsEntity> shopCar = (Map<Integer, GoodsEntity>)request.getSession().getAttribute("shopCar");
        //购物车所有商品id组成的字符数组
        Integer[] goods = shopCar.keySet().toArray(new Integer[shopCar.keySet().size()]);


        //创建订单详情表
        /*for(Integer gid:gids ) {
            OrderDetailEntity ode = new OrderDetailEntity();
            GoodsEntity goodsEntity = goodsBiz.queryGoodsDetail(gid);
            float v = goodsEntity.getGoodsNumber() * goodsEntity.getGoodsPrice();
            ode.setOdid(MyUtils.getUUID());
            ode.setOid(oe.getOid());
            ode.setOdprice(v);
            ode.setOdnumber(goodsEntity.getGoodsNumber());
        }*/
        //创建订单
        OrderEntity or = orderService.createOrder(oe,goods);
        if(or!=null){
            request.setAttribute("order",or);
            String oid=or.getOid();
            timer.schedule(new OrderTimer(orderService,oid,timer),30*60*1000);
            return "orderinfo";
        }else {
            request.setAttribute("msg","结算失败，请稍后再试");
            return "accounts";
        }

    }
    //点击支付
    @RequestMapping("toPlay")
    public String toPlay(HttpServletRequest request,String oid){
        boolean b = orderService.toPlay(oid);
        if(b){
            return "buysuccess";
        }else {
            request.setAttribute("msg","支付失败");
            return "orderinfo";
        }

    }
    //订单列表
    @RequestMapping("orderList")
    public String orderList(HttpServletRequest request){
        UserEntity user =(UserEntity) request.getSession().getAttribute("user");
        int aid = user.getAid();
        List<OrderEntity> orderList = orderService.orderList(aid);

        request.setAttribute("orderList",orderList);

        return "orderlist";
    }

    //订单详情列表
    @RequestMapping("orderListDetail")
    public String orderListDetail(String oid, HttpServletRequest request){
        List<OrderDetailEntity> list = orderService.orderDetailList(oid);
        if(list!=null){
            request.setAttribute("list",list);
            return "orderdetail";
        }
        return null;

    }



}
